<?php
include BASEPATH . "/plugins/alipay/vendor/Alipay/AopClient.php";
include BASEPATH . "/plugins/alipay/vendor/Alipay/request/AlipayTradeCreateRequest.php";
/**
 * 头条小程序
 * Class Sms
 */
class Model_Api_V2_Payment_Zhifubao
{
    public static function pay($params)
    {
        try{
            $expire = 15;
            $notify_url = self::get_notify_url();
            $config = Model_Sysconfig::get_configs(0,array('cfg_zhifubao_alipay_appid','cfg_zhifubao_alipay_private_key','cfg_zhifubao_alipay_public_key'),0);
            include BASEPATH.'plugins/zhifubao/classes/vendor/Alipay/AopClient.php';
            $aop = new AopClient ();
            $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
            $aop->appId = $config['cfg_zhifubao_alipay_appid'];
            $aop->rsaPrivateKey = $config['cfg_zhifubao_alipay_private_key'];
            $aop->alipayrsaPublicKey = $config['cfg_zhifubao_alipay_public_key'];
            $aop->apiVersion = '1.0';
            $aop->signType = 'RSA2';
            $aop->postCharset='UTF-8';
            $aop->format='json';
            include BASEPATH.'plugins/zhifubao/classes/vendor/Alipay/request/AlipayTradeCreateRequest.php';
            $request = new AlipayTradeCreateRequest();
            $bizcontent = array(
                'body' => $params['body'],
                'subject' => $params['order_title'],
                'out_trade_no' => $params['outTradeNo'],
                'timeout_express' => $expire.'m',
                'total_amount' => $params['totalprice'],
                'buyer_id' => $params['user_id'],
            );
            $request->setNotifyUrl($notify_url);
            $request->setBizContent(json_encode($bizcontent));
            //这里和普通的接口调用不同，使用的是sdkExecute
            $response = $aop->zhifubao_pay($request);
            //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题htmlspecialchars($response)
            return array('status' => true, 'data' =>  $response, 'notify_url'=>$notify_url);//就是orderString 可以直接给客户端请求，无需再做处理。
        }catch(Exception $e){
            //失败返回false
            return ['status' => false, 'data' =>  $e ];
        }
    }


    public function get_notify_url()
    {
        $domain = St_Functions::get_main_host();
        $m_main_url = $GLOBALS['cfg_m_main_url'];
        //增加手机域名为空时的判断
        if(empty($m_main_url) || $m_main_url == $domain)
        {
            $mobile_domain = $domain;
        }
        else
        {
            $mobile_domain = $m_main_url;
        }
        $notify_url = $mobile_domain . '/alipayxcx/pc/notify_url';

        return $notify_url;
    }

}
